//
// Created by 啊汤哥 on 2025/6/24.
//

#define  MaxSize 10
#include <cstdio>

typedef struct {
    int data[MaxSize];
    int top;
} SqStack;

void initStack(SqStack &s) {
    s.top = -1;
}

bool Push(SqStack &s, char value) {
    if (s.top == MaxSize - 1) {
        return false;
    }
    s.top += 1;
    s.data[s.top] = value;
    return true;
}

// 出栈
bool PopStack(SqStack &s, char &x) {
    if (s.top == -1) {
        return false;
    }
    x = s.data[s.top];
    s.top -= 1;
    return true;
}

// 判断栈是否非空
bool IsNotEmpty(SqStack s) {
    return s.top != -1;
}
// 判断栈是否为空
bool IsEmpty(SqStack s) {
    return s.top == -1;
}


bool breakCheck(char str[], int length) {
    SqStack s;
    initStack(s);

    for (int i = 0; i < length; i++) {
        if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
            Push(s, str[i]);
        } else {
            if (IsEmpty(s)) { // 应该检查栈是否为空
                return false;
            }
            char topEl;
            PopStack(s, topEl);
            if ((str[i] == ')' && topEl != '(') ||
                (str[i] == ']' && topEl != '[') ||
                (str[i] == '}' && topEl != '{')) {
                return false; // 右括号与栈顶左括号不匹配
                }
        }
    }
    return IsEmpty(s); // 栈为空则返回true，否则返回false
}



int main() {
    char str1[] = "{[()]}";
    printf("Testing str1: %s\n", str1);
    printf("Result: %s\n", breakCheck(str1, sizeof(str1) - 1) ? "Matched" : "Not Matched");

}